{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 309,
   "id": "8bf3ae7f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LogisticRegression  #逻辑回归\n",
    "from sklearn import metrics\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "\n",
    "plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']# 设置微软雅黑字体\n",
    "plt.rcParams['axes.unicode_minus'] = False # 避免坐标轴不能正常的显示负号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 310,
   "id": "51ea1e12",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv('D:/大厂私教课程/第六周作业/data(1)-data.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 311,
   "id": "9ec132c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>RowNumber</th>\n",
       "      <th>CustomerId</th>\n",
       "      <th>Surname</th>\n",
       "      <th>CreditScore</th>\n",
       "      <th>Geography</th>\n",
       "      <th>Gender</th>\n",
       "      <th>Age</th>\n",
       "      <th>Balance</th>\n",
       "      <th>HasCrCard</th>\n",
       "      <th>EstimatedSalary</th>\n",
       "      <th>Exited</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>15634602</td>\n",
       "      <td>Hargrave</td>\n",
       "      <td>619</td>\n",
       "      <td>France</td>\n",
       "      <td>Female</td>\n",
       "      <td>42</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1</td>\n",
       "      <td>101348.88</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>15647311</td>\n",
       "      <td>Hill</td>\n",
       "      <td>608</td>\n",
       "      <td>Spain</td>\n",
       "      <td>Female</td>\n",
       "      <td>41</td>\n",
       "      <td>83807.86</td>\n",
       "      <td>0</td>\n",
       "      <td>112542.58</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>15619304</td>\n",
       "      <td>Onio</td>\n",
       "      <td>502</td>\n",
       "      <td>France</td>\n",
       "      <td>Female</td>\n",
       "      <td>42</td>\n",
       "      <td>159660.80</td>\n",
       "      <td>1</td>\n",
       "      <td>113931.57</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>15701354</td>\n",
       "      <td>Boni</td>\n",
       "      <td>699</td>\n",
       "      <td>France</td>\n",
       "      <td>Female</td>\n",
       "      <td>39</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>93826.63</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>15737888</td>\n",
       "      <td>Mitchell</td>\n",
       "      <td>850</td>\n",
       "      <td>Spain</td>\n",
       "      <td>Female</td>\n",
       "      <td>43</td>\n",
       "      <td>125510.82</td>\n",
       "      <td>1</td>\n",
       "      <td>79084.10</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   RowNumber  CustomerId   Surname  CreditScore Geography  Gender  Age  \\\n",
       "0          1    15634602  Hargrave          619    France  Female   42   \n",
       "1          2    15647311      Hill          608     Spain  Female   41   \n",
       "2          3    15619304      Onio          502    France  Female   42   \n",
       "3          4    15701354      Boni          699    France  Female   39   \n",
       "4          5    15737888  Mitchell          850     Spain  Female   43   \n",
       "\n",
       "     Balance  HasCrCard  EstimatedSalary  Exited  \n",
       "0       0.00          1        101348.88       1  \n",
       "1   83807.86          0        112542.58       0  \n",
       "2  159660.80          1        113931.57       1  \n",
       "3       0.00          0         93826.63       0  \n",
       "4  125510.82          1         79084.10       0  "
      ]
     },
     "execution_count": 311,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "raw",
   "id": "caeb2dc2",
   "metadata": {},
   "source": [
    "RowNumber\t     行号\n",
    "CustomerId\t     客户ID\n",
    "Surname\t        客户姓氏   \n",
    "CreditScore   \t  信用分\n",
    "Geography\t     地理位置\n",
    "Gender\t        性别\n",
    "Age\t           年龄\n",
    "Balance\t        账户余额\n",
    "HasCrCard\t      是否有信用卡\n",
    "EstimatedSalary\t   预估薪资\n",
    "Exited\t        是否退出\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 312,
   "id": "f53c6398",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['France', 'Spain', 'Germany'], dtype=object)"
      ]
     },
     "execution_count": 312,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Geography  地理位置\n",
    "data['Geography'].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 313,
   "id": "d1aa70d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5014, 2509, 2477]"
      ]
     },
     "execution_count": 313,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.groupby('Geography')['Geography'].count().tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 314,
   "id": "ebf2e28e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:ylabel='Geography'>"
      ]
     },
     "execution_count": 314,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYsAAAD6CAYAAABQ6WtbAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Z1A+gAAAACXBIWXMAAAsTAAALEwEAmpwYAAAVcElEQVR4nO3dfbRddX3n8fenCQ+B+gAS5SFAeHCoICVt4owgClIYZM20AzgjD0UiToZOiwUV6xprqx1rsxhmxvhQWoYFJXVAW2xhqAuXCghUrYiJBhEqCAgkaXAF0AUYoyT5zh97Rw6XS/Y913vuuTf3/Vrrrpz93Xuf8z0c7v2c/dtPqSokSdqWXxp2A5Kkqc+wkCR1MiwkSZ0MC0lSJ8NCktRp9rAbGJQ99tij5s+fP+w2JGlaWbly5WNVNXdkfbsNi/nz57NixYphtyFJ00qSh0erOwwlSepkWEiSOhkWkqROhoUkqZNhIUnqZFhIkjoZFpKkTpMSFknuTnJ/+/NXbe2CJI8kuTfJST3LXpRkTZK7kixsa7OTLE+yNsntSQ6YjL4lSY3JOilvp6o6eOtEkoOA84DDgH2Bm5LsD7weOBqYD7wRuAJYAJwN7AzMA5YAy4CTJ6l3SZrxJissRt5h6RTgmqp6CrgnyUPAQuBUYHlVbQJuTDI3yZ5t/aNVVUmupgmL50lyLnAuwH777Tfm5hb+wSf7fDvq18r/efawW5D0Cxj4MFSSXYFXJHkwyS1JXkOzNdF7SvkaYK9R6mtH1qtqA7AhyW4jX6uqLquqRVW1aO7c513aRJI0TgMPi6r6cVW9uKoOBP4CuBbYEdjSs9gWYPM46pKkSTCpR0NV1WeAOcA6YJ+eWfOA1aPU96bZ6vh5PckcYFZVPTkZPUuSJmcY6iVJ9mgfnwQ8Dtw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ENT3xnDOt9vv0R8eev+lOnA8yykCZbkbcDRVbVk2L1oakryOWBpVX1l2L2MlcNQkjSJkhxOc/fPrw67l34YFpI0SdKc7f9ZmnM/ptWwjsNQkqRObllIkjoZFpKkToaFJKmTYSFJ6mRYSJI6/X+dfOpT7vAF7QAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "num = data.groupby('Geography')['Geography'].count()\n",
    "Geography = data['Geography'].unique()\n",
    "\n",
    "sns.barplot(x=Geography,y=num,)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 315,
   "id": "b6f9d36d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['Female', 'Male'], dtype=object)"
      ]
     },
     "execution_count": 315,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Gender  性别\n",
    "data['Gender'].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 316,
   "id": "36957b2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:ylabel='Gender'>"
      ]
     },
     "execution_count": 316,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYsAAAD6CAYAAABQ6WtbAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Z1A+gAAAACXBIWXMAAAsTAAALEwEAmpwYAAAR6UlEQVR4nO3de7Bd9VnG8e/ThJRAL4CcyiVAKK2t0CpO0hlbQaZaBtFRC04NIBJTMdVBYWy9tfUyOhqxttJ2pK0MkYyCM6W2lHHQKlWsdpRxggW5SBAoIYmppmAFGtM2Oa9/rJWyOZzyOztk73OS/f3M7Jmz3nV7d2af8+S31tprpaqQJOm5vGC+G5AkLXy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pTgI8A7xhcOcnivv6WqnolcDjww2Po+6DkYajJsKeqXr13or/l+xnAR7vfQV4IfLKfva2qPtcvdzfwib7+L8AP7l2G7hfztcCL6G7Fsm1gf98PvC7J3l/Ml+7n96MJUFX3JNmZZCVwBHA38N8D859KclL/fJvXAU/M2MSrgdcAt/af86V0n2PtA8NiMi0CnhoMEOgOTQFfHSjtAb7S/7ybboSyGPgn4JeADcCJdP/DG7QY+Nmq+ov93rkmzYfpRrRHAh8cnJHkp+lGv79Md/ufy2esuxi4v6pWjL7Ng5+HoSZQVX0J2J5kFUCSlyc5YY6rH0E3mvgkcCjwbbMs81ngp5IsTnJIktc/76Y1qT5Od1L75KqaeSXfa4DbquouumfezLQJOH7v568/f+Eodx8ZFpPrEuBXkjwMXD/Xlarqi8DHgAeB9wL3zLLY1cD/0p3buAc45Xl3q4lUVV8F/pxuFDvTdcDaJPfy9Ah4cN3/o/ucb0jyEPAenvmoAw3Be0NJkpocWUiSmgwLSVKTYSFJajIsJElNhoUkqcmwkCQ1GRaSpCbDQpLU9P/455p9rd3U3wAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.barplot(x=data['Gender'].unique(),y=data['Gender'].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 317,
   "id": "adc4b3cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RowNumber          0\n",
       "CustomerId         0\n",
       "Surname            0\n",
       "CreditScore        0\n",
       "Geography          0\n",
       "Gender             0\n",
       "Age                0\n",
       "Balance            0\n",
       "HasCrCard          0\n",
       "EstimatedSalary    0\n",
       "Exited             0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 317,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看缺失值 \n",
    "data.isnull().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df094bd9",
   "metadata": {},
   "source": [
    "### 转换数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 318,
   "id": "2eb47526",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = data.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 319,
   "id": "e406688a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# RowNumber  行号\n",
    "#CustomerId  客户ID\n",
    "#Surname  客户姓氏 \n",
    "# 这三列对预测没有用处 此处删除这三列\n",
    "df = df.iloc[:,3:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 320,
   "id": "6379a9fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# Geography  地理位置\n",
    "# 'France' 转换为  1\n",
    "# 'Spain'  转换为  2\n",
    "# 'Germany' 转换为 3\n",
    "\n",
    "df['Geography'] = df['Geography'].replace('France',1)\n",
    "df['Geography'] = df['Geography'].replace('Spain',2)\n",
    "df['Geography'] = df['Geography'].replace('Germany',3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 321,
   "id": "2ad7b925",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3], dtype=int64)"
      ]
     },
     "execution_count": 321,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['Geography'].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 322,
   "id": "ddde67f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "#  Gender  性别\n",
    "# 'Female' = 1\n",
    "# 'Male' = 0\n",
    "\n",
    "df['Gender'] = df['Gender'].replace('Female',1)\n",
    "df['Gender'] = df['Gender'].replace('Male',0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 323,
   "id": "ddf22276",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 0], dtype=int64)"
      ]
     },
     "execution_count": 323,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['Gender'].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 324,
   "id": "ab7e4f9c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CreditScore</th>\n",
       "      <th>Geography</th>\n",
       "      <th>Gender</th>\n",
       "      <th>Age</th>\n",
       "      <th>Balance</th>\n",
       "      <th>HasCrCard</th>\n",
       "      <th>EstimatedSalary</th>\n",
       "      <th>Exited</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>619</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>42</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1</td>\n",
       "      <td>101348.88</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>608</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>41</td>\n",
       "      <td>83807.86</td>\n",
       "      <td>0</td>\n",
       "      <td>112542.58</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>502</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>42</td>\n",
       "      <td>159660.80</td>\n",
       "      <td>1</td>\n",
       "      <td>113931.57</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>699</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>39</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>93826.63</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>850</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>43</td>\n",
       "      <td>125510.82</td>\n",
       "      <td>1</td>\n",
       "      <td>79084.10</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9995</th>\n",
       "      <td>771</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>39</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1</td>\n",
       "      <td>96270.64</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9996</th>\n",
       "      <td>516</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>35</td>\n",
       "      <td>57369.61</td>\n",
       "      <td>1</td>\n",
       "      <td>101699.77</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9997</th>\n",
       "      <td>709</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>36</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>42085.58</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9998</th>\n",
       "      <td>772</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>42</td>\n",
       "      <td>75075.31</td>\n",
       "      <td>1</td>\n",
       "      <td>92888.52</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9999</th>\n",
       "      <td>792</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>28</td>\n",
       "      <td>130142.79</td>\n",
       "      <td>1</td>\n",
       "      <td>38190.78</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10000 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      CreditScore  Geography  Gender  Age    Balance  HasCrCard  \\\n",
       "0             619          1       1   42       0.00          1   \n",
       "1             608          2       1   41   83807.86          0   \n",
       "2             502          1       1   42  159660.80          1   \n",
       "3             699          1       1   39       0.00          0   \n",
       "4             850          2       1   43  125510.82          1   \n",
       "...           ...        ...     ...  ...        ...        ...   \n",
       "9995          771          1       0   39       0.00          1   \n",
       "9996          516          1       0   35   57369.61          1   \n",
       "9997          709          1       1   36       0.00          0   \n",
       "9998          772          3       0   42   75075.31          1   \n",
       "9999          792          1       1   28  130142.79          1   \n",
       "\n",
       "      EstimatedSalary  Exited  \n",
       "0           101348.88       1  \n",
       "1           112542.58       0  \n",
       "2           113931.57       1  \n",
       "3            93826.63       0  \n",
       "4            79084.10       0  \n",
       "...               ...     ...  \n",
       "9995         96270.64       0  \n",
       "9996        101699.77       0  \n",
       "9997         42085.58       1  \n",
       "9998         92888.52       1  \n",
       "9999         38190.78       0  \n",
       "\n",
       "[10000 rows x 8 columns]"
      ]
     },
     "execution_count": 324,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc3634fc",
   "metadata": {},
   "source": [
    "### 查看相关性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 325,
   "id": "fd864110",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CreditScore       -0.027094\n",
       "Geography          0.153771\n",
       "Gender             0.106512\n",
       "Age                0.285323\n",
       "Balance            0.118533\n",
       "HasCrCard         -0.007138\n",
       "EstimatedSalary    0.012097\n",
       "Exited             1.000000\n",
       "Name: Exited, dtype: float64"
      ]
     },
     "execution_count": 325,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.corr()['Exited']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e2941d4",
   "metadata": {},
   "source": [
    "删除相关性低的列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 326,
   "id": "14b07509",
   "metadata": {},
   "outputs": [],
   "source": [
    "# EstimatedSalary    HasCrCard        CreditScore       这三列对结果的相关性太低 可以删除 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 327,
   "id": "c0987135",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = df[['Geography','Gender','Age','Balance']]\n",
    "y = df['Exited']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec14ad6b",
   "metadata": {},
   "source": [
    "划分数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c1c6b31",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=200)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f5de9b5",
   "metadata": {},
   "source": [
    "标准化数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 329,
   "id": "77724996",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 标准化\n",
    "std = StandardScaler().fit(X_train)\n",
    "\n",
    "# 标准化训练集\n",
    "X_train = std.transform(X_train)\n",
    "# 标准化测试集\n",
    "X_test = std.transform(X_test)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 330,
   "id": "ed1f2901",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.31082824, -0.91209716,  1.15401677,  0.11650245],\n",
       "       [ 0.31082824,  1.09637443,  1.43906769,  0.10632842],\n",
       "       [-0.89685798,  1.09637443, -0.74632269,  0.1012975 ],\n",
       "       ...,\n",
       "       [-0.89685798,  1.09637443, -0.27123783,  1.03698574],\n",
       "       [ 1.51851445, -0.91209716,  1.81913558,  0.63709842],\n",
       "       [ 0.31082824, -0.91209716, -0.65130572,  1.38866852]])"
      ]
     },
     "execution_count": 330,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e3345c7",
   "metadata": {},
   "source": [
    "测试检查显著性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 331,
   "id": "8fe97727",
   "metadata": {},
   "outputs": [],
   "source": [
    "import statsmodels.api as sm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 332,
   "id": "d3ee2e27",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.653868\n",
      "         Iterations 5\n"
     ]
    }
   ],
   "source": [
    "logistic = sm.Logit(y_train,X_train).fit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 333,
   "id": "527a1b48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                 Exited   No. Observations:                 8000\n",
      "Model:                          Logit   Df Residuals:                     7996\n",
      "Method:                           MLE   Df Model:                            3\n",
      "Date:                Sat, 20 Nov 2021   Pseudo R-squ.:                 -0.3022\n",
      "Time:                        15:00:11   Log-Likelihood:                -5230.9\n",
      "converged:                       True   LL-Null:                       -4017.0\n",
      "Covariance Type:            nonrobust   LLR p-value:                     1.000\n",
      "==============================================================================\n",
      "                 coef    std err          z      P>|z|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "x1             0.2144      0.025      8.651      0.000       0.166       0.263\n",
      "x2             0.1766      0.023      7.588      0.000       0.131       0.222\n",
      "x3             0.4796      0.025     19.057      0.000       0.430       0.529\n",
      "x4             0.1170      0.025      4.743      0.000       0.069       0.165\n",
      "==============================================================================\n"
     ]
    }
   ],
   "source": [
    "print(logistic.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 334,
   "id": "f90e24a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "y_predict = logistic.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 335,
   "id": "4161e4e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7444619463993531"
      ]
     },
     "execution_count": 335,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看模型得分\n",
    "metrics.roc_auc_score(y_test,y_predict)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65340c4b",
   "metadata": {},
   "source": [
    "\n",
    "使用机器学习模型建模"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 348,
   "id": "ea3c4502",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=5, estimator=LogisticRegression(max_iter=10000),\n",
       "             param_grid={'C': [0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35,\n",
       "                               0.39999999999999997, 0.44999999999999996,\n",
       "                               0.49999999999999994, 0.5499999999999999, 0.6,\n",
       "                               0.65, 0.7, 0.75, 0.7999999999999999, 0.85, 0.9,\n",
       "                               0.95, 1.0],\n",
       "                         'solver': ['liblinear', 'sag', 'newton-cg', 'lbfgs']})"
      ]
     },
     "execution_count": 348,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 设定网格搜索的参数\n",
    "param_grid = {\n",
    "    'C':list(np.linspace(0.05,1,20)),\n",
    "    'solver':['liblinear','sag','newton-cg','lbfgs']\n",
    "}\n",
    "#设定模型参数\n",
    "model = LogisticRegression(penalty='l2',max_iter=10000)\n",
    "\n",
    "#网格搜索\n",
    "grid_search = GridSearchCV(model,param_grid,cv=5)\n",
    "\n",
    "#训练模型\n",
    "grid_search.fit(X_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 337,
   "id": "0fc0ce5f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'C': 0.49999999999999994, 'solver': 'sag'}"
      ]
     },
     "execution_count": 337,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看最佳参数\n",
    "grid_search.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 338,
   "id": "fbd56f26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.795"
      ]
     },
     "execution_count": 338,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 最佳得分\n",
    "grid_search.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 349,
   "id": "b79bb37f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置最终模型\n",
    "\n",
    "# 调用最佳参数设置模型\n",
    "final_mod = LogisticRegression(C=grid_search.best_params_['C'],solver=grid_search.best_params_['solver'],max_iter=10000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 350,
   "id": "f9a4aa4b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 训练模型\n",
    "final_mod = final_mod.fit(X_train,y_train)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 351,
   "id": "194c847b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LogisticRegression(C=0.49999999999999994, max_iter=10000, solver='sag')"
      ]
     },
     "execution_count": 351,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "final_mod"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 352,
   "id": "405b7f44",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 预测数据\n",
    "y_final_predict = final_mod.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 358,
   "id": "d50b59b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, ..., 0, 0, 0], dtype=int64)"
      ]
     },
     "execution_count": 358,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "y_final_predict"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d214a21",
   "metadata": {},
   "source": [
    "模型评分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 353,
   "id": "72907ad1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.794875"
      ]
     },
     "execution_count": 353,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "final_mod.score(X_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 354,
   "id": "ea10bae9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.776"
      ]
     },
     "execution_count": 354,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "final_mod.score(X_test,y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 355,
   "id": "80267aa1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5257425138200101"
      ]
     },
     "execution_count": 355,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "metrics.roc_auc_score(y_test,y_final_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 356,
   "id": "db690933",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.776"
      ]
     },
     "execution_count": 356,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "metrics.accuracy_score(y_test,y_final_predict)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
